#include <RohmMultiSensor.h>
#include "apps.h"

#define RETRY_INIT_DELAY 2000
#define RUN_DELAY 1000
void rpr_0521rs_loop()
{
    static uint32_t last = 0;
    static int state = 0;
    static uint32_t mark = 0;

    uint8_t err = 0;
    if (millis() - last < 100)
    {
        return;
    }
    last = millis();
    switch (state)
    {
    case 0:
        if (sensorAls.init() == 0)
        {
            state = 2;
        }
        else
        {
            log_e("init Error: 1");
            state = 1;
        }
        mark = millis();

        break;
    case 1:
        if (millis() - mark < RETRY_INIT_DELAY)
        {
            break;
        }
        else
        {
            state = 0;
        }
        break;
    case 2:
        sensorAls.measure();
        // print the values to the serial port
        // log_i("%d\t%0.3f",sensorAls.prox,sensorAls.ambLight);
        if (sensorAls.err != 0)
        {
            state = 0;
            log_e("RPR_0521RS  Error %d", sensorAls.err);
        }
        else
        {
            state = 3;
        }
        mark = millis();

        break;
    case 3:
        if (millis() - mark < RUN_DELAY) // 10 * 100ms = 1s
        {
            break;
        }
        else
        {
            state = 2;
        }
        break;

    default:
        state = 0;
        break;
    }
}